<template>
  <div>
    <el-drawer title="选择撰稿" :visible.sync="select" direction="rtl" size="50%">
      <seleceDepartment :person="data.writerid" @setCanSee="setCanSee"></seleceDepartment>
      撰稿仅可设置1人，可置空
    </el-drawer>

    <el-descriptions title="新闻稿" ref="NewsBody" :column="1" :colon=false style="background-color: white;">
      <el-descriptions-item label="撰稿">：{{writer.name}}</el-descriptions-item>
      <el-descriptions-item label="内容">：</el-descriptions-item>
      <el-descriptions-item label="">
        <div v-html="data.message"></div>
      </el-descriptions-item>
    </el-descriptions>
    <el-button type="primary" icon="el-icon-edit" size="mini" v-if="canDo(2)" @click="setWriter">编辑授权</el-button>
    <el-button type="primary" icon="el-icon-edit" size="mini" v-if="canDo(1)"
      @click="()=>{this.$router.push(`/write?id=${this.$route.query.id||this.$props.id}&type=news`)}">编辑文字</el-button>
    <!-- <el-button type="primary" icon="el-icon-refresh" circle></el-button> -->
  </div>
</template>

<script>
  import messageApi from "../../api/message.js"
  import userApi from "../../api/user.js"
  import seleceDepartment from "../selectDeparment.vue"
  import Dom2pic from "dom2pic"
  import domtoimage from 'dom-to-image'
  import face from "../../api/face.js"

  export default {
    data() {
      return {
        data: {},
        writer: {},
        canWrite: false,
        select: false,
        isImgEdit: false,
        apiType: "imgbox",
        toimgDialog: false,
        faceType: 1,
        toimgUrl: ""
      }
    },
    methods: {
      refresh() {
      let id=this.$route.query.id||this.$props.id
        messageApi.getNews(id).then((res) => {
          console.log(res)
          if (res.data.length == 0) {
            this.$emit("hasNews", 0)
            return
          }
          this.data = res.data[0]
          this.$emit("hasNews", id)
          let regex2 = /\{(.+?)\]/g;
          let str = this.data.message
          if (str.match(regex2) != null) {
            for (let emo of str.match(regex2)) {
              str = str.replace(emo, String.fromCodePoint(emo.replace(/\{/, "")
                .replace(/\]/, "")));
            }
          }

          this.data.message = str

          userApi.getUserInfo(res.data[0].writerid).then((res) => {

            this.writer = res.data[0]
          })
        })
        messageApi.getCanUp(this.$attrs.id).then((res) => {
          this.canWrite = !!res.data.length
        })
      },
      canDo(num) {
        let result = false
        let userInfo = userApi.getMyInfo()
        if (userInfo.qx >= num && (userInfo.department == 3 || userInfo.department == 0)) {
          result = true
        } else if (num == 1 && (userInfo.department == 3 || userInfo.department == 0)) {
          result = this.canWrite
        }
        return result
      },
      setWriter() {
        this.select = true
      },
      setCanSee(result) {
        if (result.length > 1) {
          this.$notify.error({
            title: '错误',
            message: '撰稿仅可设置1人'
          });
        } else if (result.length == 0) {
          messageApi.setWrite(this.data.id, 0, 1).then((res) => {
            if (res.code == 1) {
              this.$notify.success({
                title: '成功',
                message: '编辑成功'
              });
            }
          })
        } else {
          messageApi.setWrite(this.data.id, result[0].id, 1).then((res) => {
            if (res.code == 1) {
              this.$notify.success({
                title: '成功',
                message: '编辑成功'
              });
            }
          })
        };
        this.refresh()
      },
    },
    created() {
      this.refresh()
    },
    components: {
      seleceDepartment
    }
  }
</script>

<style>
</style>
